/**
 * 
 */
package com.speljohan.rsbot.script.wrappers;

import java.awt.Point;

import com.speljohan.rsbot.accessors.Interface;

/**
 * This class handles the components of an interface. <br>
 * Can be used for banking/shops etc.
 * 
 * @author Qauters
 */
public class RSInterfaceComponent extends RSInterfaceChild {
	protected int componentIndex;

	/**
	 * @param parent
	 * @param index
	 * @param componentIndex
	 */
	public RSInterfaceComponent(final RSInterface parent, final int index,
			final int componentIndex) {
		super(parent, index);
		this.componentIndex = componentIndex;
	}

	/**
	 * The ID of this component
	 * 
	 * @return The ID of this component, or -1 if component == null
	 */
	@Override
	public int getComponentID() {
		final Interface component = getInterfaceInternal();
		if (component != null) {
			return component.getComponentID();
		}

		return -1;
	}

	/**
	 * The index of this component
	 * 
	 * @return The index of this component, or -1 if component == null
	 */
	public int getComponentIndex() {
		final Interface component = getInterfaceInternal();
		if (component != null) {
			return component.getComponentIndex();
		}

		return -1;
	}

	/**
	 * The name of this component
	 * 
	 * @return The name of this component, or "" if component == null
	 */
	public String getComponentName() {
		final Interface component = getInterfaceInternal();
		if (component != null) {
			return component.getComponentName();
		}

		return "";
	}

	/**
	 * The stack size of this component
	 * 
	 * @return The stack size of this component, or -1 if component == null
	 */
	public int getComponentStackSize() {
		final Interface component = getInterfaceInternal();
		if (component != null) {
			return component.getComponentStackSize();
		}

		return -1;
	}

	/**
	 * Gets the interface represented by this object.
	 * */
	@Override
	Interface getInterfaceInternal() {
		final Interface child = super.getInterfaceInternal();
		if (child != null) {
			final Interface[] components = child.getCache();
			if (components != null && componentIndex >= 0
					&& componentIndex < components.length) {
				return components[componentIndex];
			}
		}

		return null;
	}

	/**
	 * The center point of the Component
	 * 
	 * @author Fusion89k
	 * @return The center point of the Component
	 */
	public Point getPoint() {
		return new Point((getAbsoluteX() + getAbsoluteX() + getWidth()) / 2,
				(getAbsoluteY() + getAbsoluteX() + getHeight()) / 2);
	}
}
